package cn.lesile.o2o.service.impl;

import java.io.File;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import cn.lesile.o2o.dao.ShopDao;
import cn.lesile.o2o.dto.ShopExecution;
import cn.lesile.o2o.entity.Shop;
import cn.lesile.o2o.enums.ShopStateEnum;
import cn.lesile.o2o.service.ShopService;
import cn.lesile.o2o.util.ImageUtil;
import cn.lesile.o2o.util.PathUtil;


@Service
public class ShopServiceImpl implements ShopService{
	
	@Autowired
	private ShopDao shopDao;
	
	@Transactional
	public ShopExecution addShop(Shop shop, File shopImg) {
		//空值判断
		if(shop==null) {
			return new ShopExecution(ShopStateEnum.NULL_SHOP);
		}
		
		try {
			//店铺信息赋初始值
			shop.setEnableStatus(0);
			shop.setCreateTime(new Date());
			shop.setLastEditTime(new Date());
			//添加店铺信息
			int effectedNum=shopDao.insertShop(shop);
			if(effectedNum<=0) {
				throw new RuntimeException("店铺创建失败");
			}else {
				if(shopImg !=null) {
					try {
					//存储图片
					addShopImg (shop,shopImg);
					}catch(Exception e) {
						throw new RuntimeException("addShopImg error"+e.getMessage());
					}
					//更新图片地址
					effectedNum=shopDao.updateShop(shop);
					if(effectedNum<=0) {
						throw new RuntimeException("更新图片地址失败");
					}
				}
			}
		}catch(Exception e) {
			throw new RuntimeException("addShop error"+e.getMessage());
		}
		return null;
	}

	private void addShopImg(Shop shop, File shopImg) {
		// 获取shop图片目录的相对值路径
		String dest=PathUtil.getShopImgPath(shop.getShopId());
		//String shopImgAddr=ImageUtil.generateThumbnail(shopImg,dest);
		//shop.setShopImg(shopImgAddr);
	}

	public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	public ShopExecution getByEmployeeId(long employeeId) throws RuntimeException {
		// TODO Auto-generated method stub
		return null;
	}

	public Shop getByShopId(long shopId) {
		// TODO Auto-generated method stub
		return null;
	}

	public ShopExecution addShop(Shop shop, CommonsMultipartFile shopImg) throws RuntimeException {
		// TODO Auto-generated method stub
		return null;
	}

	public ShopExecution modifyShop(Shop shop, CommonsMultipartFile shopImg) throws RuntimeException {
		// TODO Auto-generated method stub
		return null;
	}

}
